package com.nes.ocr;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class VideoProcessingExample {


    public static void main(String[] args) {
        try {
            // 设置原生库路径
//            Loader.setLibraryPath("path/to/native/libs");

            // 检查视频文件路径
            File videoFile = new File("bb.mp4");
            if (!videoFile.exists()) {
                System.err.println("视频文件不存在: " + videoFile.getAbsolutePath());
                return;
            }

            // 初始化FFmpegFrameGrabber
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFile.getAbsolutePath());
            grabber.start();

            Java2DFrameConverter converter = new Java2DFrameConverter();
            while (true) {
                Frame frame = grabber.grab();
                if (frame == null) break; // 没有更多帧时停止

                // 检查Frame对象是否为空
                if (frame.image == null) {
                    System.err.println("抓取到的帧为空，跳过当前帧");
                    continue;
                }

                // 将帧转换为BufferedImage
                BufferedImage image = converter.getBufferedImage(frame);

                // 检查BufferedImage对象是否为空
                if (image == null) {
                    System.err.println("转换后的BufferedImage为空，跳过当前帧");
                    continue;
                }

                // 图像预处理
                // ...

                // ocr识别
                // ...

                // 保存图像
                File outputFile = new File("img/frame_" + System.currentTimeMillis() + ".png");
                ImageIO.write(image, "png", outputFile);
            }

            grabber.stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}